﻿using NDK.Motion.MachineManager;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.MachineResources
{
    public partial class Frm_AddPosition : Form
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Frm_AddPosition");

        public Station CurStation = new Station();
        public Position NewPos = new Position();
        public Frm_AddPosition()
        {
            InitializeComponent();
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            try
            {
                //Guid
                NewPos.Guid = Guid.NewGuid().ToString();
                //点位名称
                if (string.IsNullOrWhiteSpace(tb_PosName.Text.Trim()))
                {
                    lbl_PosNameTip.ForeColor = Color.Red;
                    return;
                }
                if (MachineDataManager.Instance.PositionList.SingleOrDefault(p => p.PositionName == tb_PosName.Text.Trim() && p.PositionName == CurStation.StationName) != null)
                {
                    lbl_PosNameTip.ForeColor = Color.Red;
                    lbl_Tip.Text = "该工站下已经存在该点位！";
                    return;
                }
                lbl_Tip.Text = "";
                lbl_PosNameTip.ForeColor = Color.Black;
                NewPos.PositionName = tb_PosName.Text.Trim();
                //执行顺序
                if (string.IsNullOrWhiteSpace(tb_PositionOrder.Text.Trim()))
                {
                    lbl_PosOrderTip.ForeColor = Color.Red;
                    return;
                }
                lbl_PosOrderTip.ForeColor = Color.Black;
                NewPos.PositionOrder = int.Parse(tb_PositionOrder.Text.Trim());
                //轴优先级
                if (string.IsNullOrWhiteSpace(tb_AxisPrioritiy.Text.Trim()))
                {
                    lbl_AxisPriorityTip.ForeColor = Color.Red;
                    return;
                }
                lbl_AxisPriorityTip.ForeColor = Color.Black;
                NewPos.AxisPriority = tb_AxisPrioritiy.Text;
                //功能类型
                NewPos.Function = tb_Function.Text;
                //点位描述
                NewPos.Description = tb_PosDescription.Text;
                //是否执行
                NewPos.IsRun = ckb_IsRun.Checked;
                NewPos.IsAllowUniform = ckb_IsAllowUniform.Checked;
                NewPos.Station = tb_StationName.Text.Trim();

                foreach (var axis in CurStation.AxisList)
                {
                    VelocityProfile vp = new VelocityProfile();
                    vp.AxisId = axis.AxisID;
                    vp.AxisName = axis.Name;
                    vp.Acc = 1000.0;
                    vp.Dec = 1000.0;
                    vp.MoveVel = 100;
                    vp.StartVel = 100;
                    vp.Pos = 0.0;
                    NewPos.VelocityProfiles.Add(vp);
                    NewPos.AxisList.Add(axis);
                }
                MachineDataManager.Instance.PositionList.Add(NewPos);
                DialogResult = DialogResult.OK;
            }
            catch (Exception)
            {
                DialogResult = DialogResult.None;
            }
        }

        private void Frm_AddPosition_Load(object sender, EventArgs e)
        {
            tb_StationName.ReadOnly = false;
            tb_StationName.Text = CurStation?.StationName;
            tb_StationName.ReadOnly = true;
        }
    }
}
